<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <style>
        #result{
            width: 400px;height: 300px;border: solid 1px #123;
        }
        li{
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        //keydown  keyup  keypress
        // window 与 可输入的元素 是可以绑定

        class App extends React.Component {
            state = {
                //发送过的数据, 存放的容器
                msgs: [
                    '别人走在拼命赚钱，而我赚不到，我感觉我和这个世界格格不入',
                    '彼岸花花开彼岸，断肠草草断肝肠',
                    '最最好的，与最最痛苦的，是一样的。'
                ]
            }

            render(){
                return <div>
                        <input type="text" onKeyDown={this.show} />
                        <hr/>
                        <div id="result" ref={el => this.result = el}>
                            <ul>
                            {
                                this.state.msgs.map((item, index) => {
                                    return <li key={index}>{item}</li>
                                })
                            }
                        </ul>
                        </div>
                    </div>
            }

            show = (e) => {
                // console.log(e.keyCode); //键盘码
                if(e.keyCode === 13 && e.ctrlKey ){//按下了回车
                    // this.result.innerHTML = e.target.value
                    //将 input 中的内容, 压入到数组中
                    let {msgs} = this.state;
                    //压入数据
                    msgs.push(e.target.value);
                    //设置 input 元素的值
                    e.target.value = '';
                    //更新一下 state
                    this.setState({
                        msgs: msgs
                    })
                }
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));

    </script>
</body>
</html>